package com.ten.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ten.common.core.entity.EduTeacher;
import com.ten.common.core.entity.TbCheckTeacher;
import com.ten.common.core.vo.Result;
import com.ten.teacher.dao.EduTeacherDao;
import com.ten.teacher.dao.TbCheckTeacherDao;
import com.ten.teacher.feign.TbMemberFeign;
import com.ten.teacher.service.TbCheckTeacherService;
import com.ten.teacher.vo.CheckStatusVo;
import com.ten.teacher.vo.CheckTeacherVo;
import com.ten.teacher.vo.CheckUpdateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @author ：Wqw
 * @date ：Created in 2022/12/7 18:51
 * @description：审核讲师的实现类
 * @modified By：
 * @version:
 */
@Service
public class TbCheckTeacherServiceImpl extends ServiceImpl<TbCheckTeacherDao, TbCheckTeacher> implements TbCheckTeacherService {

    @Autowired
    private TbCheckTeacherDao tbCheckTeacherDao;

    @Autowired
    private EduTeacherDao eduTeacherDao;

    @Autowired
    private TbMemberFeign tbMemberFeign;

    /**
     * 查询审核讲师的列表，模糊查询，分页
     * page  页码数
     * limit 当前页面的行数
     * CheckTeacherVo 模糊查询的条件
     *
     * @return
     */
    @Override
    public Result<IPage<EduTeacher>> getCheckTeacherByPage(Integer page, Integer limit, CheckTeacherVo checkTeacherVo) {
        IPage<EduTeacher> iPage = new Page<>(page, limit);
        IPage<EduTeacher> checkTeacherByPage = tbCheckTeacherDao.getCheckTeacherByPage(iPage, checkTeacherVo);
        return new Result(200, "查询审核讲师成功", checkTeacherByPage);
    }

    /**
     * 修改审核讲师信息
     * CheckUpdateVo*/
    /**修改讲师信息
     *CheckUpdateVo 条件类 */
    @Override
    public Result<EduTeacher> updateTeacherById(CheckUpdateVo checkUpdateVo) {
        //创建一个新老师接收修改条件
        EduTeacher newTeacher = new EduTeacher();
        //设置修改条件
        UpdateWrapper<EduTeacher> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", checkUpdateVo.getId());
        wrapper.set("mobile", checkUpdateVo.getMobile());
        wrapper.set("name", checkUpdateVo.getName());
        wrapper.set("email", checkUpdateVo.getEmail());
        wrapper.set("intro", checkUpdateVo.getIntro());
        wrapper.set("gmt_modified", LocalDateTime.now());
        //仅修改指定的列
        int update = eduTeacherDao.update(newTeacher, wrapper);
        if (update == 1) {
            return new Result<>(200, "修改信息成功！");
        }
        return new Result<>(500, "修改信息失败！");
    }


    /**
     * 审核讲师
     * 修改讲师的审核状态,
     * 讲师状态，启动状态（逻辑删除）,
     * 用户的类型
     * CheckVo 条件类*/
    @Override
    @Transactional
    public Result<EduTeacher> checkTeacherById(CheckStatusVo checkStatusVo) {
        //创建一个新老师接收修改条件
        EduTeacher newTeacher = new EduTeacher();
        //设置修改条件
        //因为(check_status)属性在Tb_check_teacher表中所以ct表也要修改
        UpdateWrapper<TbCheckTeacher> checkWrapper = new UpdateWrapper<>();
        //修改讲师的条件
        UpdateWrapper<EduTeacher> wrapper = new UpdateWrapper<>();
        String id = checkStatusVo.getId();
        wrapper.eq("id", id);
        checkWrapper.eq("teacher_id", id);
        String checkStatus = checkStatusVo.getCheckStatus();
        //判断条件checkStatus为1则通过审核
        if (StringUtils.hasText(checkStatus) && "1".equals(checkStatus)) {
            //审核状态改变
            checkWrapper.set("check_status", checkStatus);
            //讲师状态改变,逻辑删除状态改变,修改时间更新
            wrapper.set("is_deleted", 0);
            wrapper.set("status", "0");
            wrapper.set("gmt_modified", LocalDateTime.now());
            //执行sql语句
            tbCheckTeacherDao.update(null,checkWrapper);
            eduTeacherDao.update(newTeacher, wrapper);
            tbMemberFeign.beTeacherByDeleted(checkStatusVo.getMobile());
            return new Result<>(200, "审核通过");
        }
        if (StringUtils.hasText(checkStatus) && "0".equals(checkStatus)) {
            checkWrapper.set("check_status", checkStatus);
            wrapper.set("gmt_modified", LocalDateTime.now());
            tbCheckTeacherDao.update(null,checkWrapper);
            eduTeacherDao.update(newTeacher, wrapper);
            return new Result<>(500, "审核不通过");
        }
        if (StringUtils.hasText(checkStatus) && "2".equals(checkStatus)) {
            checkWrapper.set("check_status", checkStatus);
            wrapper.set("gmt_modified", LocalDateTime.now());
            tbCheckTeacherDao.update(null,checkWrapper);
            eduTeacherDao.update(newTeacher, wrapper);
        }
       return new Result<>(500, "待审核");
    }

}
